import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:surfaceim/common/entities/entities.dart';
import 'package:surfaceim/common/store/store.dart';
import 'package:surfaceim/common/values/values.dart';
import 'package:surfaceim/common/components/bottom_picker/bottom_picker.dart';

import '../index.dart';

class CellBox extends GetView<BasicinformationController> {
  final String _type;
  CellBox({
    Key? key,
    required String type,
  }) : this._type = type;

  //TODO : 这里这么写刑不刑啊

  @override
  Widget build(BuildContext context) {
    /// 打开picker
    _openSimpleItemPicker(
        BuildContext context, List<Text> items, String title, i) {
      int _i = int.parse(i);

      BottomPicker(
        selectedItemIndex: _i,
        buttonSingleColor: AppColors.primaryElement,
        items: items,
        title: title,
        onSubmit: (index) {
          print(index);
        },
      ).show(context);
    }

    /// 构建cell
    Widget _buildCell({String name = '', String label = '', required key, i}) {
      print(i);
      return Material(
        color: AppColors.primaryBackground,
        child: InkWell(
          child: Padding(
            padding: EdgeInsets.fromLTRB(30.w, 39.0.h, 30.w, 39.0.h),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(i.toString()),
                Text(
                  name + '：',
                  style: TextStyleSecondary(),
                ),
                Row(
                  children: [
                    Text(
                      label,
                      style: TextStyleTertiary(color: AppColors.labelText),
                    ),
                    SizedBox(
                      width: 13.w,
                    ),
                    Icon(Icons.chevron_right_rounded,
                        color: AppColors.labelText)
                  ],
                )
              ],
            ),
          ),
          onTap: () {
            UserConfig userConfig = Dictionaries.to.userConfig;
            List<Text> labelList = []; // 用于展示的数组
            if (userConfig.code.toString() == '1') {
              var res = userConfig.data;
              List<dynamic>? list = [];
              switch (key) {
                case 'workList':
                  {
                    list = res!.workList;
                  }
                  break;
                case 'professionList':
                  {
                    list = res!.professionList;
                  }
                  break;
                case 'moneyList':
                  {
                    list = res!.moneyList;
                  }
                  break;
                case 'sexList':
                  {
                    list = res!.sexList;
                  }
                  break;
                case 'workList':
                  {
                    list = res!.workList;
                  }
                  break;
                case 'eduList':
                  {
                    list = res!.eduList;
                  }
                  break;
                case 'stuList':
                  {
                    list = res!.stuList;
                  }
                  break;
              }

              list!.forEach((item) => {labelList.add(Text(item.name))});
            } else {
              Dictionaries.to.conf();
            }

            _openSimpleItemPicker(context, labelList, name, i);
          },
          splashColor: AppColors.secondaryElement,
          // highlightColor: AppColors.primaryElementSecondary,
        ),
      );
    }

    List<Widget> itemList = [];

    if (_type == 'work') {
      controller.work.forEach((element) {
        itemList.add(_buildCell(
            name: element['name'],
            label: element['label'],
            key: element['key'],
            i: element['i']));
      });
    }

    if (_type == 'shool') {
      controller.shool.forEach((element) {
        itemList.add(_buildCell(
            name: element['name'],
            label: element['label'],
            key: element['key'],
            i: element['i']));
      });
    }

    return ClipRRect(
      borderRadius: BorderRadius.circular(16),
      child: Column(
        children: itemList,
      ),
    );
  }
}
